﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using kim.Model;
namespace kim.BLL
{
    /// <summary>
    /// eg_news
    /// </summary>
    public partial class eg_news
    {
        private readonly kim.DAL.eg_news dal = new kim.DAL.eg_news();
        public eg_news()
        { }
        #region  Method
        /// <summary>
        /// 分页控制
        /// </summary>
        /// <param name="tbname">要分页显示的表名</param>
        /// <param name="FieldKey">用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段</param>
        /// <param name="PageCurrent">要显示的页码</param>
        /// <param name="PageSize">每页的大小(记录数)</param>
        /// <param name="FieldShow">以逗号分隔的要显示的字段列表,如果不指定,则显示所有字段</param>
        /// <param name="FieldOrder">以逗号分隔的排序字段列表,可以指定在字段后面指定DESC/ASC,用于指定排序顺序</param>
        /// <param name="Where">查询条件</param>
        /// <returns></returns>
        public DataSet GetNewsPager(int PageCurrent, int PageSize, string FieldShow, string FieldOrder, string Where)
        {
            return dal.GetNewsPager("eg_news", "id", PageCurrent, PageSize, FieldShow, FieldOrder, Where);
        }


        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 得到最大sotr
        /// </summary>
        public int GetMaxSort(string StrWhere)
        {
            return dal.GetMaxSort(StrWhere);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(kim.Model.eg_news model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(kim.Model.eg_news model)
        {
            return dal.Update(model);
        }

        public kim.Model.eg_news UpdateRead(int id)
        {
            Model.eg_news mo = GetModel(id);
            mo.Rcount = mo.Rcount + 1;
            Update(mo);
            return mo;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            bool del = false;
            Model.eg_news Nmo = new BLL.eg_news().GetModel(ID);
            if (Nmo != null)
            {
                if (Nmo.Pic != null && Nmo.Pic != "")
                    del = kim.Common.Utils.DeleteFile(Nmo.Pic);
                if (Nmo.Pic != null && Nmo.MaxPic != "")
                    del = kim.Common.Utils.DeleteFile(Nmo.MaxPic);
                del = dal.Delete(ID);
            }
            return del;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            return dal.DeleteList(IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public kim.Model.eg_news GetModel(int ID)
        {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public kim.Model.eg_news GetModelByCache(int ID)
        {

            string CacheKey = "eg_newsModel-" + ID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (kim.Model.eg_news)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<kim.Model.eg_news> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<kim.Model.eg_news> GetModelList(int top, string strWhere, string filedOrder)
        {
            DataSet ds = dal.GetList(top, strWhere, filedOrder);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<kim.Model.eg_news> DataTableToList(DataTable dt)
        {
            List<kim.Model.eg_news> modelList = new List<kim.Model.eg_news>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                kim.Model.eg_news model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new kim.Model.eg_news();
                    if (dt.Rows[n]["ID"] != null && dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    if (dt.Rows[n]["ClassID"] != null && dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    if (dt.Rows[n]["Name"] != null && dt.Rows[n]["Name"].ToString() != "")
                    {
                        model.Name = dt.Rows[n]["Name"].ToString();
                    }
                    if (dt.Rows[n]["zjj"] != null && dt.Rows[n]["zjj"].ToString() != "")
                    {
                        model.zjj = dt.Rows[n]["zjj"].ToString();
                    }
                    if (dt.Rows[n]["ejj"] != null && dt.Rows[n]["ejj"].ToString() != "")
                    {
                        model.ejj = dt.Rows[n]["ejj"].ToString();
                    }
                    if (dt.Rows[n]["jj"] != null && dt.Rows[n]["jj"].ToString() != "")
                    {
                        model.jj = dt.Rows[n]["jj"].ToString();
                    }
                    if (dt.Rows[n]["Content"] != null && dt.Rows[n]["Content"].ToString() != "")
                    {
                        model.Content = dt.Rows[n]["Content"].ToString();
                    }
                    if (dt.Rows[n]["Pic"] != null && dt.Rows[n]["Pic"].ToString() != "")
                    {
                        model.Pic = dt.Rows[n]["Pic"].ToString();
                    }
                    if (dt.Rows[n]["Sort"] != null && dt.Rows[n]["Sort"].ToString() != "")
                    {
                        model.Sort = int.Parse(dt.Rows[n]["Sort"].ToString());
                    }
                    if (dt.Rows[n]["InTime"] != null && dt.Rows[n]["InTime"].ToString() != "")
                    {
                        model.InTime = DateTime.Parse(dt.Rows[n]["InTime"].ToString());
                    }
                    if (dt.Rows[n]["ChannelID"] != null && dt.Rows[n]["ChannelID"].ToString() != "")
                    {
                        model.ChannelID = int.Parse(dt.Rows[n]["ChannelID"].ToString());
                    }
                    if (dt.Rows[n]["DesignerID"] != null && dt.Rows[n]["DesignerID"].ToString() != "")
                    {
                        model.DesignerID = int.Parse(dt.Rows[n]["DesignerID"].ToString());
                    }
                    if (dt.Rows[n]["Rcount"] != null && dt.Rows[n]["Rcount"].ToString() != "")
                    {
                        model.Rcount = int.Parse(dt.Rows[n]["Rcount"].ToString());
                    }
                    if (dt.Rows[n]["Author"] != null && dt.Rows[n]["Author"].ToString() != "")
                    {
                        model.Author = dt.Rows[n]["Author"].ToString();
                    }

                    if (dt.Rows[n]["EnName"] != null && dt.Rows[n]["EnName"].ToString() != "")
                    {
                        model.EnName = dt.Rows[n]["EnName"].ToString();
                    }
                    if (dt.Rows[n]["MaxPic"] != null && dt.Rows[n]["MaxPic"].ToString() != "")
                    {
                        model.MaxPic = dt.Rows[n]["MaxPic"].ToString();
                    }
                    if (dt.Rows[n]["IsIndex"] != null && dt.Rows[n]["IsIndex"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsIndex"].ToString() == "1") || (dt.Rows[n]["IsIndex"].ToString().ToLower() == "true"))
                        {
                            model.IsIndex = true;
                        }
                        else
                        {
                            model.IsIndex = false;
                        }
                    }

                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

